﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Xml.Linq;

namespace Utilities.ViewModel.HtmlHelpers
{
    /// <summary>
    /// 分页帮助类
    /// </summary>
    public static class PagingHelpers
    {
        /// <summary>
        /// 返回分页
        /// </summary>
        /// <param name="html">此方法将扩展的 HTML 帮助器实例。</param>
        /// <param name="pagingInfo">分页信息</param>
        /// <param name="pageUrl">分页地址</param>
        ///<param name="displayPages">显示页数默认10</param>
        /// <returns></returns>
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl, int displayPages = 10)
        {
            var canshu = GetUrlParameter();//已有url参数
            var result = new StringBuilder();//返回的html
            int startpag = 1;//开始页码
            int endpag = pagingInfo.TotalPages;//结束页码
            int leftDisplayPages = (int)Math.Truncate((decimal)displayPages / 2);//当前页左边显示的最大页数
            int rightDisplayPages = displayPages - leftDisplayPages - 1;//当前页右边显示的最大页数
          
            //如果当前页大于左边显示页
            if (pagingInfo.CurrentPage > leftDisplayPages + 1)
            {
                //如果右侧能显示的页小于最大右边显示页
                if (pagingInfo.TotalPages - pagingInfo.CurrentPage < rightDisplayPages)
                {
                    //计算开始页
                    startpag = pagingInfo.CurrentPage - (displayPages - (pagingInfo.TotalPages - pagingInfo.CurrentPage) - 1);
                    if (startpag < 1)
                    {
                        startpag = 1;
                    }
                }
                else
                {
                    //计算开始页
                    startpag = pagingInfo.CurrentPage - leftDisplayPages;
                    endpag = pagingInfo.CurrentPage + rightDisplayPages;
                }
            }
            else//不大于则从1开始判断结束页
            {
                if (displayPages < pagingInfo.TotalPages)
                {
                    endpag = displayPages;
                }
            }
            //上一页
            if (pagingInfo.CurrentPage > 1)
            {
                var strUrl = GetUrl(pageUrl, pagingInfo.CurrentPage - 1, canshu);
                TagBuilder liPrev = new TagBuilder("li");//在mvc3中使用TagBuilder,除了需要引用System.Web.Mvc还要引用System.Web.WebPages才能使用。
                TagBuilder tagPrev = new TagBuilder("a");//上一页
                tagPrev.MergeAttribute("href", strUrl);
                tagPrev.InnerHtml = "上一页";
                liPrev.InnerHtml = tagPrev.ToString();
                result.Append(liPrev.ToString());
            }
            //正常页码
            for (int i = startpag; i <= endpag; i++)
            {
                var strUrl = GetUrl(pageUrl, i, canshu);
                TagBuilder li = new TagBuilder("li");
                TagBuilder tag = new TagBuilder("a"); // 构建一个 <a> 标记
                tag.MergeAttribute("href", strUrl);
                tag.InnerHtml = i.ToString();
                if (i == pagingInfo.CurrentPage)
                {
                    li.AddCssClass("active");
                }
                li.InnerHtml = tag.ToString();
                result.Append(li.ToString());
            }
            //下一页
            if (pagingInfo.CurrentPage < pagingInfo.TotalPages)
            {
                var strUrl = GetUrl(pageUrl, pagingInfo.CurrentPage + 1, canshu);
                TagBuilder liNext = new TagBuilder("li");
                TagBuilder tagNext = new TagBuilder("a");//下一页
                tagNext.MergeAttribute("href", strUrl);
                tagNext.InnerHtml = "下一页";
                liNext.InnerHtml = tagNext.ToString();
                result.Append(liNext.ToString());
            }
            return MvcHtmlString.Create(result.ToString());
        }
        /// <summary>
        /// 获取url所有参数（形式：&xxx=yyy&zzz=aaa）
        /// </summary>
        /// <returns></returns>
        private static string GetUrlParameter()
        {
            var qs = System.Web.HttpContext.Current.Request.QueryString;
            var canshu = new StringBuilder();//已有url参数
            foreach (var item in qs.AllKeys)
            {
                if (item != "Page")
                {
                    canshu.Append("&");
                    canshu.Append(item);
                    canshu.Append("=");
                    canshu.Append(qs[item]);
                }
            } 
            return canshu.ToString();
        }
        /// <summary>
        /// 获取分页url地址
        /// </summary>
        /// <param name="pageUrl">分页页面地址</param>
        /// <param name="pageNumber">页码</param>
        /// <param name="urlParameter">参数</param>
        /// <returns></returns>
        private static string GetUrl(Func<int, string> pageUrl,int pageNumber, string urlParameter)
        {
            var url = pageUrl(pageNumber);
            if (url.IndexOf('?') > 0)
            {
                if (!string.IsNullOrEmpty(urlParameter.ToString()))
                {
                    urlParameter = "?" + urlParameter.Substring(1, urlParameter.Length - 1);//&换?
                }
            }
            return url + urlParameter;
        }
    }

    public static class MenuHelpers
    {
        public static MvcHtmlString Menu()
        {
            var result = new StringBuilder();//返回的html
            XElement root = XElement.Load(Common.Current.Server.MapPath(@"~/App_Data/xml/Menu.xml"));
            foreach (var item in root.Elements())
            {
                TagBuilder li = new TagBuilder("li");
                string id = item.Attribute("id").Value;
                string title = item.Attribute("title").Value;
                if (item.HasElements)
                {                    
                    li.AddCssClass("accordion-group");
                    TagBuilder a = new TagBuilder("a");
                    a.MergeAttribute("id", "sidebar-" + id);
                    a.MergeAttribute("data-toggle", "collapse");
                    a.MergeAttribute("data-parent", "#accordion3");
                    a.MergeAttribute("href", "#collapse-" + id);
                    a.InnerHtml = title;
                    TagBuilder ul2 = new TagBuilder("ul");
                    ul2.MergeAttribute("id", "collapse-" + id);
                    ul2.AddCssClass("nav nav-list collapse");
                    var lis = new StringBuilder();
                    foreach (var item2 in item.Elements())
                    {
                        string id2 = item2.Attribute("id").Value;
                        string title2 = item2.Attribute("title").Value;
                        string href2 = item2.Attribute("href").Value;
                        TagBuilder li2 = new TagBuilder("li");
                        TagBuilder a2 = new TagBuilder("a");
                        a2.MergeAttribute("id", "sidebar-" + id);
                        a2.MergeAttribute("href", href2);
                        a2.InnerHtml = title2;
                        li2.InnerHtml = a2.ToString();
                        lis.Append(li2.ToString());
                    }
                    ul2.InnerHtml = lis.ToString();
                    li.InnerHtml = a.ToString()+ul2;
                }
                else
                {
                    string href = item.Attribute("href").Value;
                    TagBuilder a = new TagBuilder("a");
                    a.MergeAttribute("id", "sidebar-" + id);
                    a.MergeAttribute("href", href);
                    a.InnerHtml = title;
                    li.InnerHtml = a.ToString();
                }
                result.Append(li.ToString());
            }
            
            return MvcHtmlString.Create(result.ToString());
        }
    }
}
